

** CONSUMPTION **

	* Variable
		summ foodckshp_e foodckshprice3dx_e foodckshprice3dxcost_e foodckshpfufu3dx_e foodckshpfufu3dxcost_e foodckshpdumboy3dx_e foodckshpdumboy3dxcost_e foodckshpgb3dx_e foodckshpgb3dxcost_e foodckshpoth3d_e foodckshpoth3dspecify_e foodckshpoth3dcost_e
		summ foodhvyoth3d_e foodhvyoth3dx_e foodhvyoth13d_e foodhvyoth13dcost_e foodhvyoth23d_e foodhvyoth23dcost_e foodhvyoth33d_e foodhvyoth33dcost_e foodhvyoth43d_e foodhvyoth43dcost_e
		summ foodbiscuits3d_e foodcandy3d_e foodpeanuts3d_e foodchips3d_e fooddonuts3d_e foodkala3d_e foodpolo3d_e foodplantain3d_e foodegg3d_e foodcassava3d_e foodpotato3d_e foodeddoes3d_e foodfish3d_e foodchicken3d_e foodmeat3d_e foodswtmeat3d_e foodpigtail3d_e

	* Sub indices
		destring foodckshpfufu3dx_e, force replace

		foreach x in phonecharge7dx foodckshprice3dx foodckshpfufu3dx foodckshpdumboy3dx foodckshpgb3dx phonecharge2wx prostitution2wx videoclub2wx cigarette2wx prostitution7dx videoclub7dx cigarette7dx {
			gen `x'ld_e = `x'_e *`x'cost_e
			move  `x'ld_e  `x'_e
		}

		foreach x in  clothes4wamt clothes2wamt shoes2wamt jewelry2wamt cellcredit2wamt cosmetics2wamt gamble2wamt schlfeeself2w med2wamt durbikeetc2w ///
		durcutlassetc2w durmattressetc2w durelectronics2w durfurniture2w durlights2w durland2w shoes4wamt jewelry4wamt cellcredit7damt cosmetics4wamt med4wamt gamble7damt schlfeeself6m schlfeeself4w schlfeeself3m{
			gen `x'ld_e = `x'_e if `x'unit_e==1
			replace `x'ld_e = `x'_e*70 if `x'unit_e==2
			move `x'ld_e  `x'_e
		}

	* P1_e3ks, recall period are different, we populate the p2 variables by converting them to a 2w recall period.

		replace  clothes2wamtld_e =  clothes4wamtld_e/2 if p1_e3wks==1
		replace  shoes2wamtld_e =  shoes4wamtld_e/2 if p1_e3wks==1
		replace  jewelry2wamtld_e =  jewelry4wamtld_e/2 if p1_e3wks==1
		replace  cosmetics2wamtld_e =  cosmetics4wamtld_e/2 if p1_e3wks==1
		replace  med2wamtld_e =  med4wamtld_e/2 if p1_e3wks==1
		replace  schlfeeself2wld_e =  schlfeeself4wld_e/2 if p1_e3wks==1
		replace  schlfeeself2wld_e = schlfeeself6mld_e/13.04 if p1_e12m==1 | p2_e12m==1 | p3_e12m==1

		replace  cellcredit2wamtld_e =  cellcredit7damtld_e*2 if p1_e3wks==1
		replace  phonecharge2wxcost_e =  phonecharge7dxcost_e*2 if p1_e3wks==1
		replace  transport2wtotcost_e =  transport7dtotcost_e*2 if p1_e3wks==1
		replace  prostitution2wxcost_e =  prostitution7dxcost_e*2 if p1_e3wks==1
		replace  videoclub2wxcost_e =  videoclub7dxcost_e*2 if p1_e3wks==1
		replace  cigarette2wxcost_e =  cigarette7dxcost_e*2 if p1_e3wks==1
		replace  gamble2wamtld_e =  gamble7damtld_e*2 if p1_e3wks==1

	* Clean high values
		foreach x in prostitution2wx_e prostitution2wxld_e cigarette2wx_e cigarette2wxld_e gamble2wdys_e gamble2wamtld_e {
			qui sum `x', d
			qui replace `x' = cond(`x'>r(p99),r(p99),`x') if `x' != .
		}

	* Set spending to zero if never did the activity
	replace prostitution2wxld_e = 0 if prostitution2wxld_e==. & prostitution2wx_e == 0
	replace cigarette2wxld_e = 0 if cigarette2wxld_e==. & cigarette2wx_e == 0
	replace gamble2wamtld_e = 0 if gamble2wamtld_e==. & gamble2wdys_e == 0

	* Generate dummies
	gen prostitution2w_dum_e = prostitution2wx_e!= 0 if !missing(prostitution2wx_e)
		la var prostitution2w_dum_e "Has been to prostitute in last 2 weeks"
	gen cigarette2w_dum_e = cigarette2wx_e>= 14 if !missing(cigarette2wx_e)
		la var cigarette2w_dum_e "Has smoked 14 cigarette in last 2 weeks"
	gen gamble2w_dum_e = prostitution2wx_e!= 0 if !missing(gamble2wdys_e)
		la var gamble2w_dum_e "Has gambled in last 2 weeks"


	* Sub Indices

		sum foodckshprice3dxld_e foodckshpfufu3dxld_e foodckshpdumboy3dxld_e foodckshpgb3dxld_e  foodckshpoth3dcost_e foodhvyoth13dcost_e foodhvyoth23dcost_e foodhvyoth33dcost_e foodhvyoth43dcost_e foodbiscuits3d_e foodcandy3d_e foodpeanuts3d_e foodchips3d_e fooddonuts3d_e foodkala3d_e foodpolo3d_e foodplantain3d_e foodegg3d_e foodcassava3d_e foodpotato3d_e foodeddoes3d_e foodfish3d_e foodchicken3d_e foodmeat3d_e foodswtmeat3d_e foodpigtail3d_e

		egen csfood3d_e = rowtotal (foodckshprice3dxld_e foodckshpfufu3dxld_e foodckshpdumboy3dxld_e foodckshpgb3dxld_e  foodckshpoth3dcost_e ///
			foodhvyoth13dcost_e foodhvyoth23dcost_e foodhvyoth33dcost_e foodhvyoth43dcost_e foodbiscuits3d_e foodcandy3d_e foodpeanuts3d_e foodchips3d_e ///
			fooddonuts3d_e foodkala3d_e foodpolo3d_e foodplantain3d_e foodegg3d_e foodcassava3d_e foodpotato3d_e foodeddoes3d_e foodfish3d_e ///
			foodchicken3d_e foodmeat3d_e foodswtmeat3d_e foodpigtail3d_e) if p2_e2wks==1 | p2_e2w2w==1 | p1_e5m ==1 | p1_e3wks ==1 | p1_e7m ==1 | p1_e12m ==1| p1_e13m ==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1  | p1_e10y==1 | p2_e10y==1 | p3_e10y==1, m
			la var csfood3d_e "Cons: Food (LD)"

		egen csnonfood2w_e = rowtotal(phonecharge2wxld_e payphone2w_e transport2wtotcost_e clothes2wamtld_e shoes2wamtld_e jewelry2wamtld_e cellcredit2wamtld_e  ///
				cosmetics2wamtld_e )  if p2_e2wks==1 | p2_e2w2w==1 | p1_e5m ==1 | p1_e3wks==1 | p1_e7m ==1 | p1_e12m ==1| p1_e13m ==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1  | p1_e10y==1 | p2_e10y==1 | p3_e10y==1, m
			la var csnonfood2w_e "Cons: Non Food (LD)"

        //prostitution2wxld_e videoclub2wxld_e cigarette2wxld_e gamble2wamtld_e usted to have this two.


		egen cshealthedu2w_e = rowtotal (med2wamtld_e schlfeeself2wld_e) if  p1_e3wks ==1 | p2_e2wks==1 | p2_e2w2w==1  | p1_e5m ==1 | p1_e7m ==1 | p1_e12m ==1|  p2_e12m==1 | p3_e12m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1  | p1_e10y==1 | p2_e10y==1 | p3_e10y==1 , m
			la var cshealthedu2w_e "Cons: Health Education (LD)"

		egen csdurable2w_e = rowtotal (durbikeetc2wld_e durcutlassetc2wld_e durmattressetc2wld_e durelectronics2wld_e durfurniture2wld_e durlights2wld_e durland2wld_e) if p2_e2wks==1 | p2_e2w2w==1   | p1_e7m ==1 | p1_e12m ==1| p1_e13m ==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1  | p1_e10y==1 | p2_e10y==1 | p3_e10y==1, m
			la var csdurable2w_e "Cons: Durable (LD)"

		egen csimpulse2w_e = rowtotal( prostitution2wxld_e videoclub2wxld_e cigarette2wxld_e gamble2wamtld_e)  if p2_e2wks==1 | p2_e2w2w==1 | p1_e5m ==1 | p1_e3wks==1 | p1_e7m ==1 | p1_e12m ==1| p1_e13m ==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1, m
			la var csimpulse2w_e "Cons: Impulse spending (LD)"

	* Indice
		gen temp = csfood3d_e*4
		egen cstot2w_e = rowtotal (temp csnonfood2w_e cshealthedu2w_e csdurable2w_e csimpulse2w_e), missing
			la var cstot2w_e  "Cons: Total (LD)"
		drop temp

	* Convert to USD

		foreach x in  csfood3d csnonfood2w cshealthedu2w csdurable2w cstot2w  {
			gen `x'usd_e = `x'_e / 70
		}

		la var csfood3dusd_e "Consumption of Food items in past 3d (USD)"
		la var csnonfood2wusd_e "Consumption of Non Food items in past 2w (USD)"
		la var cshealthedu2wusd_e "Consumption of Health and Education items in past 2w (USD)"
		la var csdurable2wusd_e "Consumption of Durable items in past 2w(USD)"
		la var cstot2wusd_e "Total Consumption in past 2w (USD)"


	* Drop unused construction
		*drop  csfood3d_e csnonfood2w_e cshealthedu2w_e csdurable2w_e


** SAVINGS **

	// Flow -- Aggregated

		sum save4wusd_e saveusdprev7d_e saveusd7d_e save4wusd_b save7dusd_b

	// Flow - Average
		egen saveusdavg7d_e = rowmean (saveusdprev7d_e saveusd7d_e) if r3==1 | r2==1 | r1==1
			la var saveusdavg7d_e "Savings: Savings Flow 7d Avg"

	// Stock --
		* Disaggregated for p2_b, p1_e2w, p1_e5m, p2_e2w2w p3_b p1_e12m
		* Aggregated for p1_b


		egen savstock_e = rowtotal (savesusuamtusd_e saveclubamtusd_e savebankamtusd_e savewpersonamtusd_e savewselfamtusd_e saveothamtusd_e) if p1_e3wks==1| p2_e2w2w==1 | p1_e5m==1 | p1_e12m ==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1
			replace savstock_e = . if round == 6
			la var savstock_e "Savings Stock (USD)"

		egen savstock_b = rowtotal (savesusuamtusd_b saveclubamtusd_b savebankamtusd_b savewpersonamtusd_b savewselfamtusd_b saveothamtusd_b) if p2==1 | p3==1
			replace savstock_b = savingsamtusd_b if p1==1
			la var savstock_b "Savings Stock (USD)"

	// Outliers
		foreach x in savstock {
			foreach p in b e {
				sum `x'_`p', d
				gen `x'p99_`p' = cond(`x'_`p'<r(p99) &`x'_`p'!=. , `x'_`p', r(p99)) if `x'_`p'!=.
			}
		}

	la var savstockp99_e "Savings Stock (USD) (capped 99th)"
	la var savstockp99_b "Savings Stock (USD) (capped 99th)"

	* Rescale savings baseline variable
	gen savstock_100p99_b = savstockp99_b/100
	la var savstock_100p99_b "Savings stock (100s of USD) (capped 99th)"
